﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DentistOffice
{
    public partial class MyAccount : System.Web.UI.Page
    {
        // DentistEntities dB;
        //Patient query;
        int patientId;
        Employee dentist;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["isLogged"] == null)
            {
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                patientId = Int32.Parse(Session["isLogged"].ToString());
            }
            using (DentistEntities dB = new DentistEntities())
            {
                // patientId = 44;
                var query = dB.Patients.First(qr => qr.id == patientId);
                // dane personalne
                var dent = dB.Employees.First(qr => qr.id == query.idDentist);
                dentist = dent;
                Imie1.Text = query.name;
                Nazwisko1.Text = query.surname;
                Email1.Text = query.email;
                Telefon1.Text = query.phone;
                DentistLabel1.Text = dent.name + " " + dent.surname;

                // dane firmowe
                NIP1.Text = query.nip.ToString();
                CompanyName1.Text = query.companyName;
                City1.Text = query.city;
                CityCode1.Text = query.cityCode;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            infoPlaceHolder.Visible = false;
            //ustawianie kontrolek
            //{ Imie1.Visible = false; ImieTextBox1.Visible = true; ImieTextBox1.Text = Imie1.Text; }
            //{ Nazwisko1.Visible = false; NazwiskoTextBox1.Visible = true; NazwiskoTextBox1.Text = Nazwisko1.Text; }
            Email1.Visible = false; EmailTextBox1.Visible = true; EmailTextBox1.Text = Email1.Text;
            Telefon1.Visible = false; TelefonTextBox1.Visible = true; TelefonTextBox1.Text = Telefon1.Text;

            DentistLabel1.Visible = false; DentistsDropDownList.Visible = true;

            using (DentistEntities dB = new DentistEntities())
            {
                ListItem item = new ListItem();
                item.Value = dentist.id.ToString();
                item.Text = dentist.name + " " + dentist.surname;
                DentistsDropDownList.Items.Add(item);
                //int c = 0;
                foreach (var dentEmp in dB.Employees.Where(qr => qr.positionId == 1 && qr.id != dentist.id))
                {
                    ListItem it = new ListItem();
                    it.Value = dentEmp.id.ToString();
                    it.Text = dentEmp.name + " " + dentEmp.surname;
                    DentistsDropDownList.Items.Add(it);
                }
            }

            //{ NIP1.Visible = false; NIPTextBox1.Visible = true; NIPTextBox1.Text = NIP1.Text; }
            //{ CompanyName1.Visible = false; CompanyNameTextBox1.Visible = true; CompanyNameTextBox1.Text = CompanyName1.Text; }
            City1.Visible = false; CityTextBox1.Visible = true; CityTextBox1.Text = City1.Text;
            Street1.Visible = false; StreetTextBox1.Visible = true; StreetTextBox1.Text = Street1.Text;
            CityCode1.Visible = false; CityCodeTextBox1.Visible = true; CityCodeTextBox1.Text = CityCode1.Text;

            chngAll.Visible = false;
            chngPass.Visible = false;

            Update.Visible = true;
            Cancel.Visible = true;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ZmienHaslo.aspx");
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            //zapisywanie zmian
            if (!Page.IsValid) return;
            using (DentistEntities dB = new DentistEntities())
            {
                var query = dB.Patients.First(qr => qr.id == patientId);

                query.email = EmailTextBox1.Text;
                query.phone = TelefonTextBox1.Text;
                query.idDentist = Int32.Parse(DentistsDropDownList.SelectedValue);
                query.city = CityTextBox1.Text;
                query.street = StreetTextBox1.Text;
                query.cityCode = CityCodeTextBox1.Text;


                dB.SaveChanges();
                Page.Response.Redirect(Page.Request.Url.ToString(), true);
            }


        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(Page.Request.Url.ToString(), true);
        }


    }
}